﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Daiz.DataBase;
using System.Data.Common;
using Daiz.WebFramework;

using Daiz.DotNet;
using System.Xml.Linq;

namespace SimPhony
{
    [DbTable("edit")]
    public class Edit : DbObject
    {
        public Edit()
        {
            Name = Difficulty = Style = Steps = Description = "";
        }

        [DbColumn("id", true)]
        public int ID { get; private set; }

        [DbColumn("songid")]
        public string SongID { get; set; }

        [DbColumn("name")]
        public string Name { get; set; }

        [DbColumn("rating")]
        public int Rating { get; set; }

        [DbColumn("difficulty")]
        public string Difficulty { get; set; }

        [DbColumn("style")]
        public string Style { get; set; }

        [DbColumn("comment1")]
        public int Comment1 { get; set; }

        [DbColumn("comment2")]
        public int Comment2 { get; set; }

        [DbColumn("steps")]
        public string Steps { get; set; }

        [DbColumn("description")]
        public string Description { get; set; }

        [DbColumn("userid")]
        public string UserID { get; private set; }

        [DbColumn("offset")]
        public int Offset { get; set; }

        [DbColumn("lastupdated")]
        public DateTime LastUpdated { get; set; }

        [DbColumn("public")]
        public bool Public { get; set; }

        [DbColumn("review")]
        public int Review { get; set; }

        [DbColumn("downloads")]
        public int Downloads { get; set; }

        [DbColumn("video")]
        public string Video { get; set; }

        [DbColumn("stats")]
        public string Stats { get; set; }

        [DbColumn("firstmeasure")]
        public int FirstMeasure { get; set; }

        public bool New(User u)
        {
            if (this.InitNew())
            {
                UserID = u.UserID;
                return true;
            }

            FirstMeasure = -1;
            return false;
        }

        public List<Arrow> Taps { get; private set; }
        public List<Arrow> Freezes { get; private set; }

        public void GenerateSteps()
        {
            Taps = new List<Arrow>() { Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None };
            Freezes = new List<Arrow>() { Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None, Arrow.None };
            string t = Steps.Substring(0, Steps.IndexOf("<FREEZ"));
            string f = Steps.Substring(Steps.IndexOf("<FREEZ"));
            XElement e = XElement.Parse(t);
            foreach (XElement x in e.Elements())
            {
                switch (x.Name.LocalName.ToLower())
                {
                    case "node":
                        Taps.Add(x.ToArrow());
                        break;
                }
            }

            e = XElement.Parse(f);
            foreach (XElement x in e.Elements())
            {
                switch (x.Name.LocalName.ToLower())
                {
                    case "node":
                        Freezes.Add(x.ToArrow());
                        break;
                }
            }
        }

        public override bool Save()
        {
            LastUpdated = DateTime.Now;
            return base.Save();
        }

        public bool Load(int id)
        {
            return KeyLoad(id);
        }

        public void AddRating(User u, int rating)
        {
            using (DbManager mgr = new DbManager())
            {
                string sql = string.Format("SELECT rating FROM ratings WHERE userid = '{0}' AND editid = {1}", u.UserID, ID);
                if (mgr.Query(sql).Count == 0)
                {
                    sql = string.Format("INSERT INTO {0} VALUES({1}, {2}, '{3}')", "ratings", ID, rating, u.UserID);
                    mgr.Insert(sql);
                }
                else
                {
                    sql = string.Format("UPDATE {0} SET rating = {1} WHERE editid = {2} AND userid = '{3}'", "ratings", rating, ID, u.UserID);
                    mgr.Update(sql);
                }

                this.Review = (int) Math.Round((from r in mgr.Query(string.Format("SELECT rating FROM {0} WHERE editid = {1}", "ratings", ID)) select Convert.ToDouble(r["rating"])).Average());
                base.Save();
            }
        }
    }
}
